package com.ty.web;

import com.ty.bean.Student;
import com.ty.service.StudentService;
import com.ty.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 田野
 */
@WebServlet(urlPatterns = "/Educational/student/checkStuNo")
public class CheckStuNoServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置字符集
        //第一句，设置服务器端编码
        resp.setCharacterEncoding("utf-8");
        //第二句，设置浏览器端解码
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        // 1、接收参数
        String stuNo = req.getParameter("stuNo");
        // 2、调用service层方法
        StudentService service = new StudentServiceImpl();
        Student student = service.findByStuNo(stuNo);
        // 3、跳转页面或者返回响应
        PrintWriter writer = resp.getWriter();
        if (student != null){
            // 通过PrinterWriter在页面中输出信息
            writer.print("学号已被注册！");
        }else {
            writer.print("学号可用！");
        }
    }
}
